<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改门店</title>
    <link rel="shortcut icon" href="favicon.ico">
    <link href="__CSS__/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="__CSS__/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="__CSS__/animate.min.css" rel="stylesheet">
    <link href="__JS__/layui/css/layui.css" rel="stylesheet">
    <link href="__CSS__/style.min.css?v=4.1.0" rel="stylesheet">
</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-8">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>修改门店</h5>
                    </div>
                    <div class="ibox-content">
                        <form class="form-horizontal m-t layui-form" id="commentForm" method="post" action="{:url('users/editShop')}">
                            <input type="hidden" value="{$shop.id}" name="id" />
                            <input type="hidden" value="1" name="role" />
                            <div class="form-group">
                                <label class="col-sm-3 control-label">门店名称：</label>
                                <div class="input-group col-sm-4">
                                    <input id="servicename" type="text" class="form-control" name="service_name" required aria-required="true" value="{$shop.service_name}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">负责人：</label>
                                <div class="input-group col-sm-4">
                                    <input id="username" type="text" class="form-control" placeholder="为空则在绑定时设置" name="user_name" aria-required="true" value="{$shop.user_name}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">手机号：</label>
                                <div class="input-group col-sm-4">
                                    <input type="tel" class="form-control layui-input" name="mobile" lay-verify="required|phone" value="{$shop.mobile}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">负责人微信</label>
                                <div class="input-group col-sm-4">
                                    <!-- <input id="openid" type="text" class="form-control" name="openid" placeholder="" value="{$shop.openid}"> -->
                                    <select lay-verify="required" name="openid" lay-filter="openid" lay-search="">
                                        <option value="">从微信用户中搜索：</option>
                                        {$shop.openid|oauthUserOption}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group layui-form-item form-inline" style="height: 60px;">
                                <input type="hidden" name="user_avatar" id="user_avatar" value="{$shop.user_avatar}"/>
                                <label class="col-sm-3 control-label">客服头像：</label>
                                <!-- <div class="input-group col-sm-2">
                                    <button type="button" class="layui-btn layui-btn-small" id="up-avatar">
                                        <i class="layui-icon"></i>上传图片</button>
                                </div> -->
                                <div class="input-group col-sm-3" id="avatar">
                                    <img src="{$shop.user_avatar}" width="40px" height="40px" />
                                </div>
                            </div>
                            <div class="form-group layui-form-item">
                                <label class="col-sm-3 control-label">销售老师：</label>
                                <div class="layui-input-inline">
                                    <select name="superior_id" id="superior_select" lay-verify="required" lay-search="">
                                        <option value="">直接选择或搜索选择</option>
                                    </select>
                                </div>
                            </div>
                            <!-- <div class="form-group">
                                <label class="col-sm-3 control-label">销售老师：</label>
                                <div class="input-group col-sm-4">
                                    <input id="username" type="text" class="form-control" placeholder="为空则在绑定时设置" name="market_user_name" aria-required="true" value="{$market.user_name ?? ''}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">销售老师手机号：</label>
                                <div class="input-group col-sm-4">
                                    <input type="tel" class="form-control layui-input" name="market_mobile" lay-verify="phone" value="{$market.mobile ?? ''}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-3 control-label">销售老师微信</label>
                                <div class="input-group col-sm-4">
                                    <select lay-verify="required" name="market_openid" lay-filter="market_openid" lay-search="">
                                        <option value="">从微信用户中搜索：</option>
                                        {:oauthUserOption($market.openid ?? '')}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group layui-form-item form-inline" style="height: 60px;">
                                <input type="hidden" name="market_user_avatar" id="market_user_avatar" value="{$market.user_avatar ?? ''}"/>
                                <label class="col-sm-3 control-label">销售老师头像：</label>
                                <div class="input-group col-sm-3" id="market_avatar">
                                    <img src="{$market.user_avatar ?? ''}" width="40px" height="40px" />
                                </div>
                            </div> -->
                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-6">
                                    <button class="btn btn-primary" type="submit">提交</button>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>

            </div>
        </div>
    </div>
    <script src="__JS__/jquery.min.js?v=2.1.4"></script>
    <script src="__JS__/bootstrap.min.js?v=3.3.6"></script>
    <script src="__JS__/content.min.js?v=1.0.0"></script>
    <script src="__JS__/plugins/validate/jquery.validate.min.js"></script>
    <script src="__JS__/plugins/validate/messages_zh.min.js"></script>
    <script src="__JS__/plugins/layer/layer.min.js"></script>
    <script src="__JS__/layui/layui.js"></script>
    <script src="__JS__/jquery.form.js"></script>
    <script type="text/javascript">
        layui.use(['form', 'upload'], function() {
            var form = layui.form;
            var upload = layui.upload;
            //执行实例
            var uploadInst = upload.render({
                elem: '#up-avatar' //绑定元素
                    ,
                url: "{:url('users/upAvatar')}" //上传接口
                    ,
                exts: 'png|jpg|jpeg|gif',
                done: function(res) {
                    //上传完毕回调
                    if (0 == res.code) {
                        $("#avatar").html('<img src="' + res.data.src + '" width="50px" height="50px">');
                        $("#user_avatar").val(res.data.src);
                    } else {
                        layer.msg(res.msg);
                    }
                },
                error: function() {
                    //请求异常回调
                }
            });

            form.on('select(role)', function(value) {
                $("#role_select").val(value.value);
                $('#superior_select').children('option').remove();
                $.getJSON(
                    "{:url('admin/users/superior')}", {
                        "role": value.value
                    },
                    (data) => {
                        $.each(data, (i, v) => {
                            $('#superior_select').append(new Option(v, i))
                        })
                        console.log($('#superior_select').children());
                        form.render("select");
                    }
                )
            });
            // form.on('select(market_openid)', function(value) {
            //     let avatar = $(value.elem).children('option:selected').data('avatar');
            //     $('#market_avatar').children('img').attr('src', avatar);
            //     $('#market_user_avatar').val(avatar);
            // });
            form.on('select(openid)', function(value) {
                let avatar = $(value.elem).children('option:selected').data('avatar');
                $('#avatar').children('img').attr('src', avatar);
                $('#user_avatar').val(avatar);
            });
        });

        var index = '';

        function showStart() {
            index = layer.load(0, {
                shade: false
            });
            return true;
        }

        function showSuccess(res) {

            layer.ready(function() {
                layer.close(index);
                if (1 == res.code) {
                    layer.alert(res.msg, {
                        title: '友情提示',
                        icon: 1,
                        closeBtn: 0
                    }, function() {
                        window.location.href = res.data;
                    });
                } else if (111 == res.code) {
                    window.location.reload();
                } else {
                    layer.msg(res.msg, {
                        anim: 6
                    });
                }
            });
        }

        $(document).ready(function() {
            // 添加管理员
            var options = {
                beforeSubmit: showStart,
                success: showSuccess
            };

            $('#commentForm').submit(function() {
                $(this).ajaxSubmit(options);
                return false;
            });
            $.getJSON(
                "{:url('admin/users/superior')}", {
                    "role": "{:$shop.role}"
                },
                (data) => {
                    $('#superior_select').children('option').first().nextAll().remove();
                    let detault = "{$shop.superior_id}";
                    $.each(data, (i, v) => {
                        let option = new Option(v, i);
                        if (detault == i) {
                            option.selected = true;
                        }
                        $('#superior_select').append(option)
                        layui.use(['form'], function() {
                            layui.form.render("select");
                        });
                    })
                }
            )
        });

        // 表单验证
        $.validator.setDefaults({
            highlight: function(e) {
                $(e).closest(".form-group").removeClass("has-success").addClass("has-error")
            },
            success: function(e) {
                e.closest(".form-group").removeClass("has-error").addClass("has-success")
            },
            errorElement: "span",
            errorPlacement: function(e, r) {
                e.appendTo(r.is(":radio") || r.is(":checkbox") ? r.parent().parent().parent() : r.parent())
            },
            errorClass: "help-block m-b-none",
            validClass: "help-block m-b-none"
        });
    </script>
</body>

</html>